# -*- encoding=utf8 -*-
__author__ = "rongmao"

import random

from airtest.core.api import *
from airtest.cli.parser import cli_setup
import logging

logger = logging.getLogger("airtest")
logger.setLevel(logging.ERROR)
if not cli_setup():
    auto_setup(__file__, logdir=True, devices=[
        "android://127.0.0.1:5037/83179b00?cap_method=JAVACAP&&ori_method=ADBORI&&touch_method=MAXTOUCH", ])

from poco.drivers.android.uiautomation import AndroidUiautomationPoco

poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)

# script content   摄像头右上角
print("start...yys")
number = 1


def xy_range(xy):
    if xy == "x":
        x = random.randint(
            200, 1500
        )
        return x
    elif xy == "y":
        y = random.randint(
            200, 500
        )
        return y
    else:
        print("error")
        os._exit(0)


x_po, y_po = 1632, 670
while True:
    start = time.time()
    # 396, 1322
    try:
        buff_position = wait(Template(r"tpl1625456832822.png", record_pos=(-0.376, 0.188), resolution=(3200, 1440)),
                             timeout=90,
                             interval=1)
    except:
        print("程序终止")
        break
    touch((random.randint(200, 800), random.randint(200, 500)), duration=random.randint(10, 30) / 100)
    time.sleep(random.randint(100, 150) / 100)
    touch((random.randint(200, 3000), random.randint(200, 1600)), duration=random.randint(10, 30) / 100)
    time.sleep(random.randint(9, 22) / 10)
    touch((random.randint(200, 800), random.randint(200, 700)), duration=random.randint(10, 30) / 100)
    time.sleep(random.randint(10, 20) / 10)
    touch((random.randint(200, 800), random.randint(200, 500)), duration=random.randint(10, 30) / 100)
    end = time.time()
    time_cost = round(end - start, 2)
    print(f"第{number}次已完成，用时:{time_cost}s")
    number += 1
    time.sleep(12)
